/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.wechat.api;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.panchitoboy.shiro.note.RequestLimit;
import com.thinkgem.jeesite.common.persistence.ResponseResult;
import com.thinkgem.jeesite.common.utils.SmsUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.bus.service.VerificationCodeService;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.LoginService;

/**
 * 注册Controller
 * @author ThinkGem
 * @version 2013-5-31
 */
@RestController
@RequestMapping(value = "${wechatPath}/rest/reg")
public class WechatRegApiController extends BaseController{
	
	@Autowired
	private LoginService loginService;
	
	@Autowired
	private VerificationCodeService verificationCodeService;
	
	@Autowired
	private UserDao userDao;

	/**
	 * 
	* @Title: savePubUser 
	* @author: ZYJ
	* @date: 2019年5月30日 上午11:13:42 
	* @Description: 注册 新增外网用户和企业信息
	* @param:  user
	* @return: ResponseResult
	* @throws
	 */
	@RequestMapping(value = "register", method = RequestMethod.POST)
	public ResponseResult register(@RequestBody User user) {
		try {
			user = this.loginService.savePubUser(user);
			if(!"".equals(user.getRemarks()) && null != user.getRemarks()){
				return ResponseResult.failure(user.getRemarks());
			}
		} catch(Exception e) {
			return ResponseResult.failure("注册失败，错误如下：" + e.getMessage());
		}
		return ResponseResult.success(user);
	}
	
	/**
	 * 
	* @Title: getCode 
	* @author: ZYJ
	* @date: 2019年5月31日 上午10:43:33 
	* @Description: 生成4位验证码
	* @return: ResponseResult
	* @throws
	 */
	@RequestMapping(value = "getCode/{mobile}/{method}", method = RequestMethod.POST)
	@RequestLimit(count = 1, time = 60000)
	public ResponseResult getCode(HttpServletRequest request, @PathVariable(value = "mobile") String mobile, @PathVariable(value = "method") String method) {
		try {
			//通过电话校验1分钟之内不能再次发送验证码
			Boolean bool = verificationCodeService.verfMinute(mobile);
			if(bool){
				return ResponseResult.failure("请不要频繁访问，稍等片刻！"); 
			}
			//获取用户
			User user = new User();
			user.setLoginName(mobile);
			User u = userDao.getByLoginName(user);
			//如果是忘记密码判断用户是否注册过
			if("forgetPassword".equals(method)){
				if(null == u){
					return ResponseResult.failure("用户不存在");
				}
			}
			//如果是更换手机号判断用户是否注册过
			if("editMobile".equals(method)){
				if(null != u){
					return ResponseResult.failure("用户已存在");
				}
			}
			//注册不需要验证，直接返回验证码
			String code = SmsUtils.getCode();
			//持久化验证码,sendCode是最终发送的验证码
			String sendCode = verificationCodeService.saveCode(code,mobile);
			SmsUtils.sendSmsCode(mobile, sendCode);
			return ResponseResult.success(sendCode);
		} catch(Exception e) {
			return ResponseResult.failure("请不要频繁访问，稍等片刻！");
		} 
	}
	
	/**
	 * 
	* @Title: editMobile 
	* @author: ZYJ
	* @date: 2019年6月3日 下午5:55:47 
	* @Description: 更换手机号
	* @param: user
	* @return: ResponseResult
	* @throws
	 */
	@RequestMapping(value = "editMobile", method = RequestMethod.POST)
	public ResponseResult editMobile(@RequestBody User user) {
		try {
			user = this.loginService.updateUser(user);
			if(!"".equals(user.getRemarks()) && null != user.getRemarks()){
				return ResponseResult.failure(user.getRemarks());
			}
		} catch(Exception e) {
			return ResponseResult.failure("换绑手机失败，错误如下：" + e.getMessage());
		}
		return ResponseResult.success(user);
	}
	
	/**
	 * 
	* @Title: confirmCode 
	* @author: ZYJ
	* @date: 2019年6月10日 下午4:30:03 
	* @Description: 忘记密码确认验证码
	* @param: user
	* @return: ResponseResult
	* @throws
	 */
	@RequestMapping(value = "confirmVerificationCode", method = RequestMethod.POST)
	public ResponseResult confirmCode(@RequestBody User user) {
		try {
			user = this.loginService.confirmCode(user);
			if(!"".equals(user.getRemarks()) && null != user.getRemarks()){
				return ResponseResult.failure(user.getRemarks());
			}
		} catch(Exception e) {
			return ResponseResult.failure("操作失败，错误如下：" + e.getMessage());
		}
		return ResponseResult.success(user);
	}
	
	/**
	 * 
	* @Title: updatePassword 
	* @author: ZYJ
	* @date: 2019年6月10日 下午7:13:24 
	* @Description: 修改密码
	* @param: @param user
	* @return: ResponseResult
	* @throws
	 */
	@RequestMapping(value = "edit/password", method = RequestMethod.POST)
	public ResponseResult updatePassword(@RequestBody User user) {
		try {
			user = this.loginService.updatePassword(user);
			if(!"".equals(user.getRemarks()) && null != user.getRemarks()){
				return ResponseResult.failure(user.getRemarks());
			}
		} catch(Exception e) {
			return ResponseResult.failure("重置密码失败，错误如下：" + e.getMessage());
		}
		return ResponseResult.success(user);
	}
	
}
